﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BankService.ServiceInterface;

namespace BankClient {

    public partial class LogInForm : Form {

        private RegistrationForm regForm;
        private BankingForm bankingForm;
        private BankServiceProxy service;

        public LogInForm() {
            InitializeComponent();

            this.service = new BankServiceProxy();

            // Events
            this.btnLogin.Click += new EventHandler(this.FormButtons_Click);
            this.btnRegister.Click += new EventHandler(this.FormButtons_Click);
            this.FormClosing += new FormClosingEventHandler(this.LogInForm_FormClosing);
        }

        private void LogInForm_FormClosing(object sender, FormClosingEventArgs e) {
            this.service.Dispose();
        }

        private void FormButtons_Click(object sender, EventArgs e) {
            Button b = sender as Button;
            if (b.Equals(this.btnRegister)) {
                this.regForm = new RegistrationForm(this, service);
                this.regForm.Show();
                this.Hide();
            } else if(b.Equals(this.btnLogin)) {
                LogInOutput logInOutput = this.service.LogIn(this.txtUsername.Text, this.txtPassword.Text);
                if (logInOutput != null) {
                    if (this.bankingForm != null)
                        this.bankingForm.Dispose();
                    this.bankingForm = new BankingForm(this.service, logInOutput, this);
                    this.bankingForm.Show();
                    this.Hide();
                } else
                    MessageBox.Show("Login was unsuccessful.");
            }
        }
    }
}
